﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=392286
(function () {
  "use strict";

  var app = WinJS.Application;
  var activation = Windows.ApplicationModel.Activation;

  app.onactivated = function (args) {

    if (args.detail.kind === activation.ActivationKind.launch) {
      if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
        // TODO: This application has been newly launched. Initialize
        // your application here.
      } else {
        // TODO: This application has been reactivated from suspension.
        // Restore application state here.
      }
    }
  };

  app.oncheckpoint = function (args) {
    // TODO: This application is about to be suspended. Save any state
    // that needs to persist across suspensions here. You might use the
    // WinJS.Application.sessionState object, which is automatically
    // saved and restored across suspension. If you need to complete an
    // asynchronous operation before your application is suspended, call
    // args.setPromise().    
  };


  // it is not allowed to show multipe message dialogs at the same time, so we created a utility function to queue them.
  var pendingAlerts = [];

  var messageDialogVisible = false;

  var showPendingAlerts = function () {

    var pendingAlert;

    if (messageDialogVisible || !pendingAlerts.length) {
      return;
    }

    messageDialogVisible = true;
    pendingAlert = pendingAlerts.shift();

    var msgPopup = Windows.UI.Popups.MessageDialog(pendingAlert.message);
    msgPopup.commands.append(new Windows.UI.Popups.UICommand('Ok', pendingAlert.callback));

    return msgPopup.showAsync().done(function () {

      messageDialogVisible = false;
      return showPendingAlerts();

    });

  };

  app.alert = function (message, callback) {

    pendingAlerts.push({
      message: message,
      callback: callback
    });

    return showPendingAlerts();

  };

app.start();
})();